from sqlalchemy import Column, Integer, String, Float, DateTime, Index, DECIMAL, UniqueConstraint
from .base import Base

class Kline5Min(Base):
    """
    合约5分钟K线数据模型
    """
    __tablename__ = "kline_5min"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    symbol = Column(String(20), nullable=False, comment='交易对符号')
    datetime = Column(DateTime, nullable=True, index=True, comment='北京时间')
    datetime_utc = Column(DateTime, nullable=True, index=True, comment='UTC时间')
    open = Column(DECIMAL(20, 8), nullable=True, comment='开盘价')
    high = Column(DECIMAL(20, 8), nullable=True, comment='最高价')
    low = Column(DECIMAL(20, 8), nullable=True, comment='最低价')
    close = Column(DECIMAL(20, 8), nullable=True, comment='收盘价')
    volume = Column(DECIMAL(20, 8), nullable=True, comment='成交量')
    amount = Column(DECIMAL(20, 8), nullable=True, comment='成交额')
    
    # 创建复合索引，用于提高查询效率
    __table_args__ = (
        # 创建symbol和datetime的唯一约束，防止重复数据
        UniqueConstraint('symbol', 'datetime', name='uq_symbol_datetime'),
        # 创建symbol和datetime的复合索引
        Index('idx_symbol_datetime', 'symbol', 'datetime'),
        # 创建symbol和datetime_utc的复合索引
        Index('idx_symbol_datetime_utc', 'symbol', 'datetime_utc'),
        # 表的其他配置
        {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8mb4', 'mysql_collate': 'utf8mb4_general_ci'}
    )
    
    def __repr__(self):
        return f"<Kline5Min(symbol='{self.symbol}', datetime='{self.datetime}')>" 